package com.allen.library.utils;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;

public class HttpConnectionUtil {
    public MyBaseDataCallBack mDataCallBack;

    //数据回调接口
    public interface MyBaseDataCallBack<T> {
        void onDataSuccessCallBack( T t);
        void onDataFailCallBack(T t,T t1);
    }


    /**
     *
     * @param urlPath
     *            下载路径
     * @param downloadDir
     *            下载存放目录
     * @return 返回下载文件
     */
    public static void downloadFile(String urlPath, String downloadDir,String token,String loginId ,String json,String fileNames,MyBaseDataCallBack<String> mDataCallBack) {
        File file = null;
        try {
            // 统一资源
            URL url = new URL(urlPath);
            // 连接类的父类，抽象类
            // 设置字符编码
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            // 设置请求头参数
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestProperty("connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("Charsert", "UTF-8");
            httpURLConnection.setRequestProperty("Token", token);
            httpURLConnection.setRequestProperty("LoginId", loginId);
            httpURLConnection.setRequestProperty("Lang", "1");
            // 打开到此 URL 引用的资源的通信链接（如果尚未建立这样的连接）。
            httpURLConnection.connect();
            String body = json;
            Log.e("body",body);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"));
            writer.write(body);
            writer.flush();
            writer.close();

            // 文件大小
            int fileLength = httpURLConnection.getContentLength();



            BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());

            String path = downloadDir + File.separatorChar + fileNames;
            Log.e("path",path);
            file = new File(path);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(file);
            int size = 0;
            int len = 0;
            byte[] buf = new byte[1024];
            while ((size = bin.read(buf)) != -1) {
                len += size;
                out.write(buf, 0, size);
                // 打印下载百分比
                // System.out.println("下载了-------> " + len * 100 / fileLength +
                // "%\n");
            }
            bin.close();
            out.close();
            mDataCallBack.onDataSuccessCallBack(file.getAbsolutePath());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            mDataCallBack.onDataFailCallBack("404","4");
            e.printStackTrace();
        } catch (IOException e) {
            mDataCallBack.onDataFailCallBack("404","4");
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
//            if (file!)
//            Log.e("down",file.getAbsolutePath());


        }

    }

    public static void main(String[] args) {



    }

}
